package com.spring.sunflower.anchor;

import android.widget.Toast;

import com.blankj.utilcode.util.ToastUtils;
import com.spring.sunflower.App;
import com.spring.sunflower.base.BaseObserver;
import com.spring.sunflower.base.BasePresenter;
import com.spring.sunflower.bean.AnchorDetailBean;
import com.spring.sunflower.bean.AnchorGiftReceivedBean;
import com.spring.sunflower.bean.BlackHasBean;
import com.spring.sunflower.bean.CommonBean;
import com.spring.sunflower.bean.LabelBean;
import com.spring.sunflower.bean.LiveApplyChatBean;
import com.spring.sunflower.constant.Constant;

import java.util.List;

import io.reactivex.rxjava3.core.Observable;

/**
 * Created by Kevin on 2021/6/2<br/>
 * <p>
 * Describe:<br/>
 */
public class AnchorInfoPresenter extends BasePresenter<AnchorContract.AnchorInfoView> {
    public AnchorInfoPresenter(AnchorContract.AnchorInfoView baseView) {
        super(baseView);
    }

    public void getAnchorDetail(String targetId) {
        Observable<AnchorDetailBean> observable = requestDataSource.requestAnchorDetail(targetId);
        addDisposable(observable, new BaseObserver<AnchorDetailBean>(baseView) {

            @Override
            public void onSuccess(AnchorDetailBean bean) {
                AnchorDetailBean.DatasBean datas = bean.getDatas();
//                baseView.hideLoading();
                baseView.showAnchorData(datas);
            }

            @Override
            public void onError(int status, String msg) {
                Toast.makeText(App.getContext(),msg+"",Toast.LENGTH_LONG).show();
            }
        });
    }

    public void getGiftLogReceived(String anchorId, String pageIndex) {
        Observable<AnchorGiftReceivedBean> observable = requestDataSource.requestGiftLogReceived(anchorId, pageIndex);
        addDisposable(observable, new BaseObserver<AnchorGiftReceivedBean>(baseView) {

            @Override
            public void onSuccess(AnchorGiftReceivedBean bean) {
                List<AnchorGiftReceivedBean.DatasBean> datas = bean.getDatas();
//                baseView.hideLoading();
                baseView.showGiftReceived(datas);
            }

            @Override
            public void onError(int status, String msg) {
                Toast.makeText(App.getContext(),msg+"",Toast.LENGTH_LONG).show();
            }
        });
    }

    public void getHasFollowed(String anchorId) {
        Observable<CommonBean> observable = requestDataSource.requestFollowedHas(anchorId);
        addDisposable(observable, new BaseObserver<CommonBean>(baseView) {

            public void onSuccess(CommonBean bean) {
                String datas = bean.getDatas();
                if ("1".equals(datas)) {
                    baseView.hasFollowed(true);
                } else {
                    baseView.hasFollowed(false);
                }
            }

            @Override
            public void onError(int status, String msg) {
                Toast.makeText(App.getContext(),msg+"",Toast.LENGTH_LONG).show();
            }
        });
    }

    public void setFollowed(String anchorId, String set) {
        Observable<CommonBean> observable = requestDataSource.requestFollowedSet(anchorId, set);
        addDisposable(observable, new BaseObserver<CommonBean>(baseView) {

            public void onSuccess(CommonBean bean) {
                baseView.setFollowedSuccess();
            }

            @Override
            public void onError(int status, String msg) {
                baseView.setFollowedFailed();
            }
        });
    }

    public void getHasBlacked(String blackMemberId, String operatorId) {
        Observable<BlackHasBean> observable = requestDataSource.requestHasBlack(blackMemberId, operatorId);
        addDisposable(observable, new BaseObserver<BlackHasBean>(baseView) {

            public void onSuccess(BlackHasBean bean) {
                String datas = bean.getDatas().getData();
                if ("1".equals(datas)) {
                    baseView.hasBlacked(true);
                } else {
                    baseView.hasBlacked(false);
                }
            }

            @Override
            public void onError(int status, String msg) {
                Toast.makeText(App.getContext(),msg+"",Toast.LENGTH_LONG).show();
            }
        });
    }

    public void blackAdd(String blackRememberId) {
        Observable<CommonBean> observable = requestDataSource.requestAddBlack(blackRememberId);
        addDisposable(observable, new BaseObserver<CommonBean>(baseView) {

            public void onSuccess(CommonBean bean) {
                baseView.blackAddSuccess();
            }

            @Override
            public void onError(int status, String msg) {
                baseView.blackAddFailed();
            }
        });
    }

    public void blackRemove(String blackRememberId) {
        Observable<CommonBean> observable = requestDataSource.requestRemoveBlack(blackRememberId);
        addDisposable(observable, new BaseObserver<CommonBean>(baseView) {

            public void onSuccess(CommonBean bean) {
                baseView.blackRemoveSuccess();
            }

            @Override
            public void onError(int status, String msg) {
                baseView.blackRemoveFailed();
            }
        });
    }

    public void getReportType() {
        Observable<LabelBean> observable = requestDataSource.requestLabel("accusationType");
        addDisposable(observable, new BaseObserver<LabelBean>(baseView) {

            @Override
            public void onSuccess(LabelBean bean) {
//                baseView.hideLoading();
                baseView.showReportType(bean.getDatas());
            }

            @Override
            public void onError(int status, String msg) {
                Toast.makeText(App.getContext(),msg+"",Toast.LENGTH_LONG).show();
            }
        });
    }

    /**
     * @param userId   本人id
     * @param anchorId 对方id
     */
    public void applyChat(String userId, String anchorId) {
        String gender = getField(Constant.KEY_GENDER, "1");
        Observable<LiveApplyChatBean> observable;
        if ("1".equals(gender)) {
            observable = requestDataSource.liveApplyChat(userId, anchorId);
        } else {
            observable = requestDataSource.liveApplyChatAnchor(anchorId);
        }
        addDisposable(observable, new BaseObserver<LiveApplyChatBean>(baseView) {
            @Override
            public void onSuccess(LiveApplyChatBean bean) {
                baseView.liveApplySuccess();
            }

            @Override
            public void onError(int status, String msg) {
                Toast.makeText(App.getContext(),msg+"",Toast.LENGTH_LONG).show();
            }
        });
    }

    public void sayHello(String anchorId,String userId) {
        Observable<BlackHasBean> observable = requestDataSource.greeted(anchorId,userId);
        addDisposable(observable, new BaseObserver<BlackHasBean>(baseView) {

            @Override
            public void onSuccess(BlackHasBean bean) {
//                baseView.hideLoading();
                baseView.sayHelloSuccess();
            }

            @Override
            public void onError(int status, String msg) {
                Toast.makeText(App.getContext(),msg+"",Toast.LENGTH_LONG).show();
            }
        });
    }
}
